package com.fxkj.cloud.drms.domain.device.repository;

import com.fxkj.cloud.dbs.common.constants.DeviceStatusEnum;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/10 15:02
 */
@Getter
public class ProductStatusStatistics {

    private final Long onlineCount;
    private final Long workingCount;
    private final Long offlineCount;
    private final Long alarmCount;
    private final Long deviceCount;

    public ProductStatusStatistics(Long onlineCount, Long workingCount, Long offlineCount, Long alarmCount) {
        this.onlineCount = onlineCount;
        this.workingCount = workingCount;
        this.offlineCount = offlineCount;
        this.alarmCount = alarmCount;
        this.deviceCount = onlineCount + workingCount + offlineCount + alarmCount;
    }

    public static ProductStatusStatistics deviceStatusStatistics(List<DeviceStatusStatistics> deviceStatusStatisticsList) {
        if (deviceStatusStatisticsList.isEmpty()) {
            return new ProductStatusStatistics(0L, 0L, 0L, 0L);
        }
        Map<DeviceStatusEnum, DeviceStatusStatistics> deviceStatusMap = deviceStatusStatisticsList.stream().collect(Collectors.toMap(DeviceStatusStatistics::getStatus, status -> status));
        return new ProductStatusStatistics(
                deviceStatusMap.getOrDefault(DeviceStatusEnum.ONLINE, DeviceStatusStatistics.defaultDeviceStatusStatistics()).getNum(),
                deviceStatusMap.getOrDefault(DeviceStatusEnum.WORKING, DeviceStatusStatistics.defaultDeviceStatusStatistics()).getNum(),
                deviceStatusMap.getOrDefault(DeviceStatusEnum.OFFLINE, DeviceStatusStatistics.defaultDeviceStatusStatistics()).getNum(),
                deviceStatusMap.getOrDefault(DeviceStatusEnum.ALARMING, DeviceStatusStatistics.defaultDeviceStatusStatistics()).getNum()
        );
    }
}

